package com.community.util;

import com.community.base.BaseMapper;
import com.community.common.Const;
import com.community.common.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 统一方法公共类
 */
public class MapperUtil {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(MapperUtil.class);

    /**
     * 条件查询
     * @param paramMap
     * @param baseMapper
     * @return
     */
    public static ServerResponse<PageInfo> pageInfo(Map paramMap, BaseMapper baseMapper){
        int pageNum = NumberUtil.safeToNumber(paramMap.get("pageNum"),1);
        int pageSize = NumberUtil.safeToNumber(paramMap.get("pageSize"),10);
        PageHelper.startPage(pageNum,pageSize,StringUtil.valueOfString(paramMap.get("order")));
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Map> resultList = baseMapper.selectByParamMap(paramMap);
        PageInfo pageInfo = new PageInfo(resultList);
        pageInfo.setList(resultList);
        long endTime = System.currentTimeMillis();    //获取结束时间
        logger.info("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        return ServerResponse.createBySuccess(Const.Msg.SELECTSUCCESS,pageInfo);
    }

    /**
     * id查询版本号是否变更
     * @param id
     * @param baseMapper
     * @return
     */
    public static boolean selectVersionIsChangeById(Integer id,int oldVersion,BaseMapper baseMapper){
        if(oldVersion != baseMapper.selectVersionById(id)){
            return false;
        }
        return true;
    }
}
